@extends('layouts.public')

@section('head')
    <link rel="stylesheet" href="{{cdn('js/plugins/webuploader/single.css')}}">
    <link rel="stylesheet" href="{{cdn('css/bootstrap-select.css')}}">
    <script src="{{cdn('js/bootstrap-select.js')}}"></script>
    <style>
        .bootstrap-select  .dropdown-menu > li > a{
            background:#fff;
            color:#333;
        }
        .bootstrap-select .dropdown-menu{
            background:#fff
        }
        .shop,.sizes {
            display: none;
        }
        #items span{
            display: inline-block;
            vertical-align: middle;
            font-size:12px;
            font-weight:bold;
            margin-right:5px;
        }
        #items input{
            width:120px;
            display: inline-block;
        }
    </style>
@endsection

@section('bodyattr')class="gray-bg"@endsection

@section('body')
    <div class="wrapper wrapper-content">
        <div class="row m-b">
            <div class="col-sm-12">
                <div class="tabs-container">
                    <ul class="nav nav-tabs">
                        <li><a href="{{route('admin.exchange.exchanges')}}">兑换商品列表</a></li>
                        <li @if(!isset($user))class="active"@endif><a href="{{route('admin.exchange.exchanges.add')}}">新增</a>
                        </li>
                        @if(isset($user))
                            <li class="active"><a href="#">编辑</a></li>
                        @endif
                    </ul>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <form action="{{route('admin.exchange.exchanges.save')}}" method="post"
                              class="form-horizontal ajaxForm">
                            <input type="hidden" name="id" value="{{$user->id or 0}}"/>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">类型</label>
                                <div class="col-sm-10">
                                    <div class="input-group m-t-xs-2">
                                        <input type="radio" name="type" value="1" required
                                               @if ((isset($user->type) ? $user->type : '') == '1') checked="checked"@endif/>商品
                                        <input type="radio" name="type" value="2" required
                                               @if ((isset($user->type) ? $user->type : '') != '1') checked="checked"@endif/>红包
                                    </div>
                                </div>
                            </div>

                            @if ((isset($user->type) ? $user->type : '') == '1')
                                <div class="form-group shop" style="display: block">
                                    <label class="col-sm-2 control-label">店铺</label>
                                    <div class="col-sm-3">
                                        <select name="shop_id" class="selectpicker show-tick form-control" data-live-search="true" id="shop_id">
                                            <option value="">请选择店铺</option>
                                            @foreach ($shops as $shop)
                                                <option value="{{$shop['id']}}"
                                                        @if ((isset($user->shop_id) ? $user->shop_id : '') == $shop['id']) selected="selected"@endif>
                                                    &nbsp;&nbsp;&nbsp;&nbsp;{{$shop['title']}}</option>

                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group shop" style="display: block">
                                    <label class="col-sm-2 control-label">商品</label>
                                    <div class="col-sm-3">
                                        <select name="good_id" class="selectpicker show-tick form-control" data-live-search="true" id="good_id">
                                            <option value="">请选择商品</option>

                                        </select>
                                    </div>
                                </div>
                                <div class="form-group shop">
                                    <label class="col-sm-2 control-label">金额</label>
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" value="{{$user->money}}" name="money"
                                               placeholder="请输入金额">
                                    </div>
                                </div>
                                @if(isset($user) && $user->size != '')
                                <div class="form-group sizes" style="display: block">
                                    <label class="col-sm-2 control-label">兑换型号</label>
                                    <div class="col-sm-5"  id="items">

                                            @foreach(json_decode($user['size'],true) as $g)
                                                <div class="item"><span class="name"> {{$g['size_name']}}</span><input type="hidden" name="size_name[]" value=" {{$g['size_name']}}"><input type="text" name="size_num[]"  value="{{ $g['size_num']}}" required class="form-control size_num"></div>
                                            @endforeach


                                    </div>
                                </div>
                                @endif


                            @endif
                            @if ((isset($user) ? $user->type : '') == '2')

                            <div class="form-group money">
                                <label class="col-sm-2 control-label">红包金额</label>
                                <div class="col-sm-3">
                                    <input type="text" class="form-control" value="{{$user->money}}" name="money"
                                           placeholder="请输入红包金额">
                                </div>
                            </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">红包封面</label>
                                    <div class="col-sm-10" id="imgupload">
                                        <div id="filePicker">选择图片</div>
                                        @if(isset($user) && $user->img != '')

                                                <div class="img-div">
                                                    <img src="{{get_file_url($user->img) }}"/>
                                                    <span class="cancel">×</span>
                                                </div>

                                        @endif

                                        <input type="hidden" name="img"  required id="img" value="{{ $user['img'] or '' }}"/>


                                        <input type="hidden" name="file_id" id="file_id" value="0"/>
                                    </div>

                                </div>
                            @endif
                            @if((isset($user->type) ? $user->type : '')=='')

                                <div class="form-group money">
                                    <label class="col-sm-2 control-label">红包金额</label>
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" name="money" placeholder="请输入红包金额">
                                    </div>
                                </div>
                                <div class="form-group money">
                                    <label class="col-sm-2 control-label">红包封面</label>
                                    <div class="col-sm-10" id="imgupload">
                                        <div id="filePicker">选择图片</div>
                                        @if(isset($user) && $user->img != '')

                                            <div class="img-div">
                                                <img src="{{get_file_url($user->img) }}"/>
                                                <span class="cancel">×</span>
                                            </div>

                                        @endif

                                        <input type="hidden" name="img"  required id="img" value="{{ $user['img'] or '' }}"/>


                                        <input type="hidden" name="file_id" id="file_id" value="0"/>
                                    </div>

                                </div>
                                <div class="form-group shop">
                                    <label class="col-sm-2 control-label">店铺</label>
                                    <div class="col-sm-3">
                                        <select name="shop_id" class="selectpicker show-tick form-control" data-live-search="true" id="shop_id">
                                            <option value="">请选择店铺</option>
                                            @foreach ($shops as $shop)
                                                <option value="{{$shop['id']}}"
                                                        @if ((isset($user->shop_id) ? $user->shop_id : '') == $shop['id']) selected="selected"@endif>
                                                    &nbsp;&nbsp;&nbsp;&nbsp;{{$shop['title']}}</option>

                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group shop">
                                    <label class="col-sm-2 control-label">商品</label>
                                    <div class="col-sm-3">
                                        <select name="good_id" class="selectpicker show-tick form-control" data-live-search="true" id="good_id">
                                            <option value="">请选择商品</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group shop">
                                    <label class="col-sm-2 control-label">金额</label>
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" value="{{$user->money or 0}}" name="money"
                                               placeholder="请输入金额">
                                    </div>
                                </div>
                            @endif
                            <div class="form-group">
                                <label class="col-sm-2 control-label">截止时间</label>
                                <div class="col-sm-3">
                                    <input type="text" required class="form-control" name="endDate" id="endDate"
                                           @if(isset($user)) value="{{$user->endDate or ''}}" placeholder="请选择时间"
                                           @endif autocomplete="off"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">所需积分</label>
                                <div class="col-sm-3">
                                    <input type="text" class="form-control" name="score" required
                                           @if(isset($user)) value="{{$user->score or ''}}" placeholder="请填写积分"
                                           @endif autocomplete="off"/>
                                </div>
                            </div>
                            <div class="form-group sizes">
                                <label class="col-sm-2 control-label">兑换型号</label>
                                <div class="col-sm-5"  id="items">

                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">可兑换总数</label>
                                <div class="col-sm-3">
                                    <input type="text" class="form-control" name="exchange_nums" required
                                           @if(isset($user)) value="{{$user->rent_nums or ''}}" placeholder="请填写数量"
                                           @endif autocomplete="off" @if(isset($user)&&$user->size) readonly @endif/>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-primary" type="submit">保存</button>
                                    <button class="btn btn-white" type="button" id="backBtn">返回</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('script')
    <script type="text/javascript" src="{{cdn('js/plugins/laydate/laydate.js')}}"></script>
    <script type="text/javascript">
        $(function () {
            laydate.render({
                elem: '#endDate',
                type: 'datetime',
                min: 0
                , max: 30
            });
            $('input[name="type"]').on('change', function () {
                var type = $(this).val();
                if (type == 1) {
                    $('.shop').show();
                    $('.money').hide();
                    $(function(){
                        $(".selectpicker" ).selectpicker('refresh');
                    })
                } else {
                    $('.shop,.sizes').hide();
                    $('.money').show();
                }
                $(function(){
                    $(".selectpicker" ).selectpicker('refresh');
                })
            })
            $("#shop_id").on('change',function(){
                var shop_id = $(this).val();

                $.ajax({
                    url:'{{route("admin.exchange.shops")}}',
                    data:{
                        shop_id:shop_id
                    },
                    type:"GET",
                    success:function (res) {
                        if(res.status==1){
                            var str = "<option value=\"\">请选择商品</option>";
                            var exchanges ={!! $user->id or 0 !!};
                            console.log(exchanges);
                            if(res.data.length>0){
                                for (var i = 0; i < res.data.length; i++) {
                                    if(exchanges&&res.data.id==exchanges.good_id){
                                        str += ' <option value="' + res.data[i].id + '" selected>' + res.data[i].title + '</option>';
                                    }else{
                                        str += ' <option value="' + res.data[i].id + '">' + res.data[i].title + '</option>';
                                    }

                                }
                            }
                            $('#good_id').html(str);
                            $("#good_id" ).selectpicker('refresh');
                        }


                    }
                })
            })
        })


    </script>
    <script src="{{cdn('js/plugins/webuploader/webuploader.nolog.min.js')}}"></script>
    <script src="{{cdn('js/plugins/webuploader/webuploader_public.js')}}"></script>
    <script type="text/javascript">
        jQuery(function ($) {
            singleUpload({
                _token: '{{csrf_token()}}',
                type_key: 'FT_ARTICLE_IMG',
                item_id: '{{$user->id or 0}}',
                pick: 'filePicker',
                boxid: 'imgupload',
                file_path: 'img',
                file_id: 'file_id',
            });
            $('#imgupload').find('.img-div>span').click(function () {
                sUploadDel($(this), 'default_img');
            });
        });
    </script>
@endsection